<?php

setlocale(LC_ALL, "ru_RU.UTF-8");
mb_internal_encoding("UTF-8");

require_once ("./config/setup.php");
require_once ("./libs/db.class.php");
require_once ("./libs/utils.inc.php");
$sm = new SSmarty();
$db = new DB();

$curRealDir = mb_substr($_SERVER["SCRIPT_FILENAME"], 0, mb_strrpos($_SERVER["SCRIPT_FILENAME"], "/"));
$curRealDir = mb_substr($curRealDir, 0, mb_strrpos($curRealDir, "/"));
$curSiteDir = "http://" . $_SERVER["SERVER_NAME"] . mb_substr($_SERVER["PHP_SELF"], 0, mb_strrpos($_SERVER["PHP_SELF"], "/"));
$curPage = mb_substr($_SERVER["SCRIPT_NAME"], mb_strrpos($_SERVER["SCRIPT_NAME"], "/") + 1);

$sm->assign("curSiteDir", $curSiteDir);
$sm->assign("curPage", $curPage);

if (!isset($_GET["action"])) {
    $db->dbq("
                Select
                    photoid as photoid,
                    maintext as maintext
                From
                    photos
                Order By
                    postdate DESC
            ");


    $currentPhotos = array();
    while ($arr = $db->assoc()) {
        $currentPhotos[] = $arr;
    }

    $currentPhotos = stripRecursive($currentPhotos);

    $sm->assign("currentPhotos", $currentPhotos);
} elseif ($_GET["action"] == "change") {
    if (preg_match("'^[0-9]+'$iu", $_GET["photoid"])) {
        $db->dbq("
                    Select
                        *
                    From
                        photos
                    Where
                        photoid = \"" . $_GET["photoid"] . "\"
                ");
        $currentPhoto = $db->assoc();
        $currentPhoto = stripRecursive($currentPhoto);

        $sm->assign("currentPhoto", $currentPhoto);
    } else {
        $sm->assign("redirectPage", $curPage);
        $sm->assign("error", "Неверное значение идентификатора фотографии");
        $sm->display("reset.html");
        exit(0);
    }
} elseif ($_GET["action"] == "delete") {
    if (preg_match("'^[0-9]+'$iu", $_GET["photoid"])) {

        $db->dbq("
                    Select
                        photoid as photoid,
                        maintext as maintext
                    From
                        photos
                    Where
                        photoid = \"" . $_GET["photoid"] . "\"
                ");
        $currentPhoto = $db->assoc();

        $currentPhoto = stripRecursive($currentPhoto);

        $sm->assign("currentPhoto", $currentPhoto);
    } else {
        $sm->assign("redirectPage", $curPage);
        $sm->assign("error", "Неверное значение идентификатора фотографии");
        $sm->display("reset.html");
        exit(0);
    }
}

if (isset($_POST["photoAddEnter"])) {
    $_POST = escapeRecursive($_POST);

    $resultPath = "";

    foreach ($_FILES as $filePostName => $fileProps) {
        if ($fileProps['error'] == 0) {
            if (substr($fileProps['type'], 0, 5) == "image") {
                $imagePath = getNextImagePath($curRealDir, $fileProps['name']);
                if (move_uploaded_file($fileProps['tmp_name'], $curRealDir . $imagePath)) {
                    $resultPath = $imagePath;
                } else {
                    $sm->assign("redirectPage", $curPage);
                    $sm->assign("error", "can not copy file. hosting problem.");
                    $sm->display("reset.html");
                    exit(0);
                }
            } else {
                $sm->assign("redirectPage", $curPage);
                $sm->assign("error", "extension not supported.");
                $sm->display("reset.html");
                exit(0);
            }
        } else {
            $sm->assign("redirectPage", $curPage);
            $sm->assign("error", "error uploading the file.");
            $sm->display("reset.html");
            exit(0);
        }
    }

    $db->dbq("
                Insert
                Into
                    photos
                Values
                    (
                        0,
                        \"" . $resultPath . "\",
                        \"" . $_POST["maintext"] . "\",
                        Now()
                    )
             ");

    $sm->assign("redirectPage", $curPage);
    $sm->display("reset.html");
    exit(0);
} elseif (isset($_POST["photoChangeEnter"])) {
    $_POST = escapeRecursive($_POST);

    $db->dbq("
                Update
                    photos
                Set
                    maintext = \"" . $_POST["maintext"] . "\",
                    postdate = Now()
                Where
                    photoid = \"" . $_POST["photoid"] . "\"
             ");

    foreach ($_FILES as $filePostName => $fileProps) {
        if ($fileProps['error'] == 0) {
            if (substr($fileProps['type'], 0, 5) == "image") {
                $imagePath = getNextImagePath($curRealDir, $fileProps['name']);
                if (move_uploaded_file($fileProps['tmp_name'], $curRealDir . $imagePath)) {
                    $db->dbq("
                                Select
                                    path
                                From
                                    photos
                                Where
                                    photoid = \"" . $_POST["photoid"] . "\"
                             ");
                    $arr = $db->row();

                    if ($arr[0] != "" && file_exists($curRealDir . $arr[0])) {
                        unlink($curRealDir . $arr[0]);
                    }
                    
                    $db->dbq("
                                Update
                                    photos
                                Set
                                    path = \"" . $imagePath . "\"
                                Where
                                    photoid = \"" . $_POST["photoid"] . "\"
                             ");
                } else {
                    $sm->assign("redirectPage", $curPage);
                    $sm->assign("error", "can not copy file. hosting problem.");
                    $sm->display("reset.html");
                    exit(0);
                }
            } else {
                $sm->assign("redirectPage", $curPage);
                $sm->assign("error", "extension not supported.");
                $sm->display("reset.html");
                exit(0);
            }
        } else {
            $sm->assign("redirectPage", $curPage);
            $sm->assign("error", "error uploading the file.");
            $sm->display("reset.html");
            exit(0);
        }
    }

    $sm->assign("redirectPage", $curPage);
    $sm->display("reset.html");
    exit(0);
} elseif (isset($_POST["photoDeleteEnter"])) {
    $_POST = escapeRecursive($_POST);

    $db->dbq("
                Select
                    path
                From
                    photos
                Where
                    photoid = \"" . $_POST["photoid"] . "\"
             ");
    $arr = $db->row();

    if ($arr[0] != "" && file_exists($curRealDir . $arr[0])) {
        unlink($curRealDir . $arr[0]);
    }

    $db->dbq("
                Delete
                From
                    photos
                Where
                    photoid = \"" . $_POST["photoid"] . "\"
             ");

    $sm->assign("redirectPage", $curPage);
    $sm->display("reset.html");
    exit(0);
}

$sm->assign("design", $sm->template_dir);
$sm->display("photos.html");
?>